package myaddressbook;

import java.awt.Choice;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 * 增加用户界面
 * @author Little-Grey
 *
 */
public class AddUser implements ActionListener{
		public static int z=2;
		public static int y=0;
		public JLabel Lname;
		public JLabel Lsex;
		public JLabel Lbirthday;
		public JLabel Lphone;
		public JLabel Lemail;
		public JLabel Lother;
		public JLabel Lnote;
		public JTextField name = new JTextField(10);
		public JTextField phone = new JTextField(15);
		public JTextField email = new JTextField(15);
		public JTextField other = new JTextField(15);
		public Choice Cbirthyear = new Choice();
		public Choice Cbirthmonth = new Choice();
		public Choice Cbirthday = new Choice();
		public Choice Csex = new Choice();
		public JButton certain;
		public JButton cancel;
		public JFrame frame=new JFrame("录入联系人信息");
		public AddUser(){
			Csex.addItem("male");
			Csex.addItem("female");
			Cbirthyear.addItem("1994");
			Cbirthyear.addItem("1995");
			Cbirthyear.addItem("1996");
			Cbirthyear.addItem("1997");
			Cbirthyear.addItem("1998");
			Cbirthyear.addItem("1999");
			Cbirthyear.addItem("2000");
			Cbirthmonth.addItem("01");
			Cbirthmonth.addItem("02");
			Cbirthmonth.addItem("03");
			Cbirthmonth.addItem("04");
			Cbirthmonth.addItem("05");
			Cbirthmonth.addItem("06");
			Cbirthmonth.addItem("07");
			Cbirthmonth.addItem("08");
			Cbirthmonth.addItem("09");
			Cbirthmonth.addItem("10");
			Cbirthmonth.addItem("11");
			Cbirthmonth.addItem("12");
			Cbirthday.addItem("01");
			Cbirthday.addItem("02");
			Cbirthday.addItem("03");
			Cbirthday.addItem("04");
			Cbirthday.addItem("05");
			Cbirthday.addItem("06");
			Cbirthday.addItem("07");
			Cbirthday.addItem("08");
			Cbirthday.addItem("09");
			Cbirthday.addItem("10");
			Cbirthday.addItem("11");
			Cbirthday.addItem("12");
			Cbirthday.addItem("13");
			Cbirthday.addItem("14");
			Cbirthday.addItem("15");
			Cbirthday.addItem("16");
			Cbirthday.addItem("17");
			Cbirthday.addItem("18");
			Cbirthday.addItem("19");
			Cbirthday.addItem("20");
			Cbirthday.addItem("21");
			Cbirthday.addItem("22");
			Cbirthday.addItem("23");
			Cbirthday.addItem("24");
			Cbirthday.addItem("25");
			Cbirthday.addItem("26");
			Cbirthday.addItem("27");
			Cbirthday.addItem("28");
			Cbirthday.addItem("29");
			Cbirthday.addItem("30");
			Cbirthday.addItem("31");
			}
		
		public void addUser(){
		Container c=frame.getContentPane();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500,300);
		frame.setLayout(new GridLayout(5,1,5,5));
		frame.setVisible(true);
		Lname = new JLabel("姓名：");
		Lsex = new JLabel("性别");
		Lbirthday = new JLabel("生日：");
		Lphone = new JLabel("电话：");
		Lemail = new JLabel("Email：");
		Lother = new JLabel("住址：");
		certain = new JButton("确定");
		cancel = new JButton("取消");
		JPanel pane1=new JPanel();
		JPanel pane2=new JPanel();
		JPanel pane3=new JPanel();
		JPanel pane4=new JPanel();
		JPanel pane5=new JPanel();
		JPanel pane6=new JPanel();
		JPanel pane7=new JPanel();
		c.add(pane1);
		c.add(pane5);
		c.add(pane2);
		c.add(pane6);
		c.add(pane3);
		c.add(pane7);
		c.add(pane4);
		pane1.add(Lname);
		pane1.add(name);
		pane5.add(Lsex);
		pane5.add(Csex);
		pane2.add(Lbirthday);
		pane2.add(Cbirthyear);
		pane2.add(Cbirthmonth);
		pane2.add(Cbirthday);
		pane6.add(Lphone);
		pane6.add(phone);
		pane3.add(Lemail);
		pane3.add(email);
		pane7.add(Lother);
		pane7.add(other);
	    pane4.add(certain);
	    pane4.add(cancel);
	    certain.addActionListener(new ActionListener()                  
	    		{
	    	public void actionPerformed(ActionEvent e)            
	    	{
	    		if(name.getText().equals(""))
	    			JOptionPane.showMessageDialog(null,"请填写姓名！",
	    					"录入结果",JOptionPane.INFORMATION_MESSAGE);
	    		else{
	    			try {
	    				FileWriter AddressBook=new FileWriter("D:\\AddressBook.txt",true);
	    				PrintWriter add=new PrintWriter(AddressBook);
	    				String s1,s2,s3,s4;
	    				if(name.getText().equals(""))s1="无";
	    				else s1=name.getText();
	    				if(phone.getText().equals(""))s2="无";
	    				else s2=phone.getText();
	    				if(email.getText().equals(""))s3="无";
	    				else s3=email.getText();
	    				if(other.getText().equals(""))s4="无";
	    				else s4=other.getText();
	    				add.println(s1+'\t'+Csex.getSelectedItem()+
	    						'\t'+Cbirthyear.getSelectedItem()+
	    						' '+Cbirthmonth.getSelectedItem()+
	    						' '+Cbirthday.getSelectedItem()+
	    						'\t'+s2+'\t'+s3+'\t'+s4);
	    				add.close();
	    				AddressBook.close();
	    				z=1;
	    				} catch (IOException e1) {
	    					// TODO Auto-generated catch block
	    					e1.printStackTrace();
	    					}
	    			if(y==0){
	    				JOptionPane.showMessageDialog(null,"新建成功",
	    						"新建结果",JOptionPane.INFORMATION_MESSAGE);
	    				}
	    			else {
	    				JOptionPane.showMessageDialog(null,"修改成功",
	    						"修改结果",JOptionPane.INFORMATION_MESSAGE);
	    				}
	    			name.setText("");
	    			phone.setText("");
	    			email.setText("");
	    			other.setText("");
	    			}
	    		}
	    	});
	    cancel.addActionListener(new ActionListener()  
	    		{
	    	public void actionPerformed(ActionEvent e)               
	    	{
	    		frame.dispose();
				z=0;
				}
	    	});
	    }
		public void actionPerformed(ActionEvent e) {
			new AddUser().addUser();
			}
		}
